package com.imay.live.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.imay.live.R;
import com.imay.live.activity.ProfileActivity;
import com.imay.live.utils.GlideUtil;

/**
 * Created by liangzhenxiong on 2017/04/07 0007.
 */

public class CelebrityLinearLayout extends LinearLayout implements View.OnClickListener {
    private ImageView iv_inf_img0, iv_inf_img1, iv_inf_img2;
    private TextView tv_name0, tv_name1, tv_name2;
    private long uid0, uid1, uid2;
    private Paint paint;
    private Path path;

    public CelebrityLinearLayout(Context context) {
        super(context);
        init(context);
    }

    public CelebrityLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CelebrityLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public CelebrityLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    void init(Context context) {
        View view0 = LayoutInflater.from(context).inflate(R.layout.item_celebrity_head, this, false);
        view0.setId(R.id.iv_liveinfo0_id);
        iv_inf_img0 = (ImageView) view0.findViewById(R.id.iv_inf_img);
        tv_name0 = (TextView) view0.findViewById(R.id.tv_name);
        view0.setOnClickListener(this);
        addView(view0);
        View view1 = LayoutInflater.from(context).inflate(R.layout.item_celebrity_head, this, false);
        iv_inf_img1 = (ImageView) view1.findViewById(R.id.iv_inf_img);
        tv_name1 = (TextView) view1.findViewById(R.id.tv_name);
        view1.setId(R.id.iv_liveinfo1_id);
        view1.setOnClickListener(this);
        addView(view1);
        View view2 = LayoutInflater.from(context).inflate(R.layout.item_celebrity_head, this, false);
        iv_inf_img2 = (ImageView) view2.findViewById(R.id.iv_inf_img);
        tv_name2 = (TextView) view2.findViewById(R.id.tv_name);
        view2.setId(R.id.iv_liveinfo2_id);
        view2.setOnClickListener(this);
        addView(view2);
    }

    public void setImg(String img0, String img1, String img2) {
        if (!TextUtils.isEmpty(img0)) {
            iv_inf_img0.setVisibility(VISIBLE);
            GlideUtil.loadCircleBorderImage(iv_inf_img0,
                    img0,
                    R.dimen.size_2dp,
                    R.color.line_f2,
                    R.mipmap.pnl_00_head108);
        } else {
            iv_inf_img0.setVisibility(INVISIBLE);
        }
        if (!TextUtils.isEmpty(img1)) {
            iv_inf_img1.setVisibility(VISIBLE);
            GlideUtil.loadCircleBorderImage(iv_inf_img1,
                    img1,
                    R.dimen.size_2dp,
                    R.color.line_f2,
                    R.mipmap.pnl_00_head108);
        } else {
            iv_inf_img1.setVisibility(INVISIBLE);
        }
        if (!TextUtils.isEmpty(img2)) {
            iv_inf_img2.setVisibility(VISIBLE);
            GlideUtil.loadCircleBorderImage(iv_inf_img2,
                    img2,
                    R.dimen.size_2dp,
                    R.color.line_f2,
                    R.mipmap.pnl_00_head108);
        } else {
            iv_inf_img2.setVisibility(INVISIBLE);
        }
    }

    public void setText(String name0, String name1, String name2) {
        tv_name0.setText(name0);
        tv_name1.setText(name1);
        tv_name2.setText(name2);
    }

    public void setUids(long uid0, long uid1, long uid2) {
        this.uid0 = uid0;
        this.uid1 = uid1;
        this.uid2 = uid2;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (path == null) {
            path = new Path();
            paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setAntiAlias(true);
            paint.setStrokeWidth(Resources.getSystem().getDisplayMetrics().density * 1);
            paint.setColor(getResources().getColor(R.color.bg_e5));
        }
        path.reset();
        path.moveTo(0, (float) (canvas.getHeight() + 0.1));
        path.lineTo(canvas.getWidth(), (float) (canvas.getHeight() + 0.1));
        canvas.drawPath(path, paint);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_liveinfo0_id) {
            if (uid0 == 0) {
                return;
            }
            ProfileActivity.start(getContext(), uid0);
        } else if (id == R.id.iv_liveinfo1_id) {
            if (uid1 == 0) {
                return;
            }
            ProfileActivity.start(getContext(), uid1);
        } else if (id == R.id.iv_liveinfo2_id) {
            if (uid2 == 0) {
                return;
            }
            ProfileActivity.start(getContext(), uid2);
        }
    }
}
